// 引入fly
import Fly from 'flyio/dist/npm/wx'
import store from '@/store/index.js'

let fly = new Fly()

// const host = process.env.NODE_ENV === "development" ? "模拟地址" : "真实地址"
// 设置超时
fly.config.timeout = 5000
// 设置请求基地址 http://c.wanwucloud.cn/
// fly.config.baseURL = 'https://api.wanwucloud.cn/api'
fly.config.baseURL = 'http://192.168.1.46:8009/api'
// 添加请求拦截器
fly.interceptors.request.use(request => {
	let userInfo = uni.getStorageSync('userInfo')
	let token = userInfo.token
	if (token) {
		request.headers['AuthorToken'] = token
	}
	uni.showLoading({
		mask: true,
		title: "加载中",
	})
	return request
}, function(error) {
	return Promise.reject(error)
})

// 添加响应拦截器
fly.interceptors.response.use(res => {
	// console.log(res, 'response');
	if (res.data.code == 200) {
		uni.hideLoading()
		return res
	} else if (res.data.code == 401) {
		uni.hideLoading()
		uni.showToast({
			icon: 'none',
			title: res.data.msg
		})
		store.commit('user/REMOVE_USER_INFO')
		uni.reLaunch({
			url: '/pages/login/login'
		})
	} else {
		uni.hideLoading()
		uni.showToast({
			icon: 'none',
			title: res.data.msg
		})
		return Promise.reject(res)
	}
}, error => {

	uni.hideLoading()
	return Promise.reject(error)
})

export default fly